package me.like.diandian.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

import androidx.databinding.DataBindingUtil;

import io.reactivex.functions.Consumer;
import me.like.diandian.R;
import me.like.diandian.base.BaseActivity;
import me.like.diandian.databinding.ViewTitleBinding;

import static com.jakewharton.rxbinding2.view.RxView.clicks;

/**
 * Created by shousakai on 2019/6/20.
 */

public class TitleView extends RelativeLayout {
    private Context mContext;
    public ViewTitleBinding binding;

    public String getTitle() {
        return title;
    }

    String title;

    public TitleView setLeftimgtype(int leftimgtype) {
        this.leftimgtype =leftimgtype;
        if (this.leftimgtype == 1) {
            setLeftImg(R.mipmap.left_img);
        } else if (this.leftimgtype == 2) {
            setLeftImg(R.mipmap.left_img2);
        }
        return this;
    }


    int leftimgtype;

    public TitleView(Context context) {
        this(context, null);
    }

    public TitleView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }


    private void initView() {
        binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.view_title, this, true);
        binding.setTitle("");
        clicks(binding.ivLeft).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                if (mContext instanceof BaseActivity) {
                    ((BaseActivity) mContext).finish();
                }
            }
        });
    }

    public void setLeftImg(int resId) {
        binding.ivLeft.setImageResource(resId);
    }

    public void setTitle(String title) {
        this.title = title;
        if (TextUtils.isEmpty(title)) {
            binding.setTitle(null);
        } else {
            binding.setTitle(title);
        }
    }


}
